<?php

/**
* @current headline.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2017年2月27日 下午5:07:44
*/

namespace haibao\jiepai\business;
use \haibao\jiepai\data\HeadLine as HeadLineData; 
use haibao\cms\business\Celebrity as CelebrityBusiness;
use haibao\cms\model\filter\Celebrity as CelebrityFilter;
use haibao\cms\model\filter\CelebrityImage as CelebrityImageFilter;
use haibao\frame\data\query\Condition;

class HeadLine extends Base{

    public static $categorySelect = array(
        3 =>'头条-置顶位',
        2 =>'头条-信息流',
        1 =>'焦点图',
    );
    public static $typeSelect = array(
        1 =>'文章',
        5 =>'网页',
        3 =>'人物主页',
    );

    public static function getHeadLineList($model,$isGetOne = false){
        $headLineData = new HeadLineData();
        if($isGetOne){
            return $headLineData->getOne($model);
        }else{
            return $headLineData->getAll($model);
        }
    }
    
    public static function delHeadLineById($id,$arrSet){
        $headLineData = new HeadLineData();
        if(!$id){
            throw new BusinessException('Id不可为空!');
        }
        $headLineData->updateById($id, $arrSet);
    }
    
    public static function getHeadlineListById($headlineId){
        if(!$headlineId){
            throw new BusinessException('Id不可为空!');
        }
        $headLineData = new HeadLineData();
        return $headLineData->getOneById($headlineId);
    }
    
    public static function addHeadline($model){
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_HEADLINE_ADD);
        self::checkHeadLineModelData($model);
        $headLineData = new HeadLineData();
        $headLineData->addHeadlineData($model);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_HEADLINE_ADD, $model->toArray(true,true));
    }
    
    public static function editHeadline($model){
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_HEADLINE_EDIT);
        self::checkHeadLineModelData($model);
        $headLineData = new HeadLineData();
        $headLineData->updateHeadLineData($model);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_HEADLINE_EDIT, $model->toArray(true,true));
    }
    
    private static function checkHeadLineModelData($model){
        if(!$model){
            throw new BusinessException('error');
        }
    }
    public static function getArticleInfoById($id){
        if(!$id){
            throw new BusinessException('error');
        }
        $headLineData = new HeadLineData();
        return $headLineData->getArticleInfoById($id);
    }
    public static function getFashionInfo($id){
        if(!$id){
            throw new BusinessException('error');
        }
        $celebrityFilter = new CelebrityFilter;
        $celebrityFilter->select(array(
            CelebrityFilter::CONDITION_FIELD_NAME_ID,
            CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
            CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE,
        ));
        $celebrityImageFilter = new CelebrityImageFilter();
        $celebrityImageFilter->select(array(
            $celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID
        ));
        $celebrityImageFilter->where($celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE);
        $celebrityFilter->leftjoin($celebrityImageFilter,null,$celebrityFilter::CONDITION_FIELD_NAME_ID,$celebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,$celebrityFilter::CONDITION_FIELD_NAME_CELEBRITY_IMAGE,false);
        $imageFilter = new \haibao\cms\model\filter\ImageLibrary();
        $imageFilter->select(array(
            $imageFilter::CONDITION_FIELD_NAME_FILENAME
        ));
        $celebrityFilter->leftjoin($imageFilter,$celebrityImageFilter,$celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,$imageFilter::CONDITION_FIELD_NAME_ID,$celebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_Url,false);
        $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_IS_APP_USE, Condition::CONDITION_EQUAL, \haibao\cms\model\data\Celebrity::ISAPPUSE_ENABLE);
        $celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $id);
        $celebrities = CelebrityBusiness::getAll($celebrityFilter);
        $result  = array();
        if($celebrities){
            foreach($celebrities as $key=>$val){
                $result['Id'] = $val->Id;
                $result['TagName'] = $val->NameEn;
                if($val->NameCn){
                    $result['TagName'] .= '('.$val->NameCn.')';
                }
                $result['Avatar'] = \haibao\cms\business\control\Base::getImageLink($val->CelebrityImage->ImageUrl->Filename, 160, 160);
            }
        }
        return $result;
    }
    
    public static function updateUpIndexById($id,$arrSet){
        $headLineData = new HeadLineData();
        if(!$id){
            throw new BusinessException('Id不可为空!');
        }
        $headLineData->updateById($id, $arrSet);
    }
    public static function cmsCheckInfoHeadline($focusId,$index,$UpTime,$DownTime){
       $headLineData = new HeadLineData();
       return $headLineData->cmsCheckInfoHeadline($focusId,$index,$UpTime,$DownTime);
    }
    //下架焦点图
    public static function cmsDownFocus($focusId){
        $headLineData = new HeadLineData();
        $headLineData->cmsDownFocus($focusId);
    }
}